/** feof ferror clearerr(f)*/
#include<errno.h>
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main(void)
{
//FILE *fopen(const char *path,const char *mode);
//int fclose(FILE *stream);
	FILE *f=NULL;
	int ret;
	char buf[1024];
	f=fopen("./test.txt","w+");
	if(NULL==f){
		perror("fopen error");
		return 1;
	}

	ret=fwrite("Hello world\n",1,12,f);
	if(12>ret){
		printf("fwrite error");
		fclose(f);
		return 1;
	}

	ret=fseek(f,0,SEEK_SET);
	if(-1==ret){
		printf("fseek error");
		fclose(f);
		return 1;
	}

	ret=fread(buf,1,12,f);
	if(12>ret){
		if(ferror(f)){
			printf("fread error\n");
			fclose(f);
			return 1;
		}else{
			if(feof(f)){
				printf("fread end-of-file\n");
				fclose(f);
				return 1;
			}
		}
		clearerr(f);
	}

	printf("read file:%s",buf);
	printf("success\n");
	fclose(f);
	return 0;


}
